﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Smallerp.Contracts;
using SmallERP.Bll.PinBan;
using SmallERP.PinBan.PB.Presenter;
using Smallerp.Contracts.DataContracts;
using System.Runtime.InteropServices;
using System.IO;

namespace SmallERP.PinBan.PB.Froms
{
    public partial class FrmSeachPhoto : Form
    {
        public FrmSeachPhoto()
        {
            InitializeComponent();
        }

        public IOperateDB _OperateDb { get; set; }
        OrderBLL _bll;

        [DllImport("kernel32.dll")]
        public static extern int WinExec(string exeName, int operType);

        [DllImport("shell32.dll ")]
        public static extern int ShellExecute(IntPtr hwnd, StringBuilder lpszOp, StringBuilder lpszFile, StringBuilder lpszParams, StringBuilder lpszDir, int FsShowCmd);


        private void FrmSeachPhoto_Load(object sender, EventArgs e)
        {
            _bll = new OrderBLL(DataTransfer.Addr, DataTransfer.Port, _OperateDb);
            dateTimePickerSTtime.Value = DateTime.Now.AddDays(-3);
            dateTimePickerEndTime.Value = DateTime.Now.AddDays(1);
        }

        private void buttonPhotoOK_Click(object sender, EventArgs e)
        {
            
            this.Cursor = Cursors.WaitCursor;
            try
            {
                List<PhotoSeach_DCT> list = _bll.GetSeachPhoto(textBoxPhotoname.Text, dateTimePickerSTtime.Value.Date, dateTimePickerEndTime.Value.Date);
               
                GcPhotoInfo.DataSource = list;
                GcPhotoInfo.RefreshDataSource();
                if (list.Count == 0)
                {
                    MessageBox.Show("未找到照片，请查看是否删除", "", MessageBoxButtons.OK, MessageBoxIcon.Information); 
                }
            }
            catch
            {

            }
            finally {
                this.Cursor = Cursors.Default;
            }
        }

        private void gvPhotoInfo_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if (e.Info.IsRowIndicator)
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
            }
        }

        private void gvPhotoInfo_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            if (e.Column.FieldName == "PicPath")
            {
                string fileName = ((PhotoSeach_DCT)gvPhotoInfo.GetFocusedRow()).PicPath;
                int exists=0;
                if (File.Exists(fileName))
                {
                    if (fileName.Contains("jpg"))
                    {
                        int ret = WinExec(@"explorer /n,/select," + fileName, 1);
                    }
                    exists=1;
                }
                string ToFileName = ((PhotoSeach_DCT)gvPhotoInfo.GetFocusedRow()).ToPath;

                if (File.Exists(ToFileName))
                {
                    int ret = WinExec(@"explorer /n,/select," + ToFileName, 1);
                    exists=1;
                }
                if (exists == 0)
                {
                    MessageBox.Show("未找到照片，请查看是否删除", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            if (e.Column.FieldName == "BoardPath")
            {
                string fileName = ((PhotoSeach_DCT)gvPhotoInfo.GetFocusedRow()).BoardPath;
                if (File.Exists(fileName))
                {
                    if (fileName.Contains("jpg"))
                    {
                        int ret = WinExec(@"explorer /n,/select," + fileName, 1);
                    }
                }
                else
                {
                    MessageBox.Show("未找到大板，请查看是否删除","",MessageBoxButtons.OK,MessageBoxIcon.Information);
                }

            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
           List<BoardInfoDCT> list= _bll.GetBoardInfo(textBoxBoardInfo.Text.Trim(), dateTimePickerPbStart.Value.Date, dateTimePickerPBEnd.Value.Date.AddDays(1));
           gridControlBoard.DataSource = list;
           gridControlBoard.RefreshDataSource();
        }

        private void gridView2_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if (e.Info.IsRowIndicator)
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
            }
        }

        private void gridView2_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            //if (e.RowHandle >=0)
            //{
            //    if (e.Column.Name.Equals("gridColumn19"))
            //    {

            //    }

 
            //}
        }
    }
}
